/**
 * @Author: Jackey 327156719@qq.com
 * @Date: 2025-06-26 15:40:13
 * @LastEditors: Jackey 327156719@qq.com
 * @LastEditTime: 2025-06-26 17:06:54
 * @FilePath: src/main/java/org/springblade/common/utils/ivrcall/mns/MyMessageListener.java
 * @Description: 这是默认设置,可以在设置》工具》File Description中进行配置
 */
package org.springblade.common.utils.ivrcall.mns;
import com.alicom.mns.tools.MessageListener;
import com.aliyun.mns.model.Message;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.admin.service.IRescueNoticeTaskService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class MyMessageListener implements MessageListener {

	@Resource
	private IRescueNoticeTaskService rescueNoticeTaskService;

    private final Gson gson = new Gson();

    @Override
    public boolean dealMessage(Message message) {
        try {
            String body = message.getMessageBodyAsString();
            log.info("收到消息，MessageId: {}, Body: {}", message.getMessageId(), body);

//            Map<String, Object> contentMap = gson.fromJson(body, HashMap.class);
//
//            // 示例：打印关键字段
//            String callId = (String) contentMap.get("call_id");
//            String statusCode = (String) contentMap.get("status_code");
//            String outId = (String) contentMap.get("out_id");
//
//            log.info("CallId: {}, StatusCode: {}, OutId: {}", callId, statusCode, outId);

            // TODO: 根据业务需求处理消息，如通知 处理业务等
			rescueNoticeTaskService.onCallResult(body);

            return true; // 返回true表示消息处理成功，MNS会删除该消息
        } catch (Exception e) {
            log.error("消息处理异常", e);
//            return false; // 返回false表示处理失败，消息会重试
			return true; // 失败不重试
        }
    }
}
